﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MMS.DAO;

namespace MMS
{
    public partial class UserControlCustomerList : UserControl
    {
        CustomerDAO customerDAO = new CustomerDAO(Param.ConnectionString.Value);
        public UserControlCustomerList()
        {
            InitializeComponent();
        }

        private void UserControlCustomerList_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            gridViewCustomer.OptionsBehavior.AutoPopulateColumns = false;
            PopulateData();
        }

        private void PopulateData()
        {
            
            List<CustomerDTO> customerList = new List<CustomerDTO>();
            customerList = customerDAO.SelectAll();
            gridControlCustomer.DataSource = customerList;
            Id.FieldName = "Id";
           CustomerName.FieldName = "Name";
            Active.FieldName = "Active";
            Email.FieldName = "Email";
            Website.FieldName = "Website";
            Phone1.FieldName = "Phone1";
            Phone2.FieldName = "Phone2";
        }

        private CustomerDTO getSelectedCustomer()
        {
            CustomerDTO selectedCustomer = null;
         
            string customerId = gridViewCustomer.GetFocusedRowCellValue(Id).ToString();
            selectedCustomer = customerDAO.Select(customerId);
            return selectedCustomer;
        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string itemName = e.Item.Name;
            if (itemName.Equals(bbiAdd.Name))
            {
                using (FormCustomerInfo f = new FormCustomerInfo())
                {
                    f.ShowDialog();
                    PopulateData();
                }
            }
            else if (itemName.Equals(bbiEdit.Name))
            {
                CustomerDTO selectedCustomer = getSelectedCustomer();
                using (FormCustomerInfo f = new FormCustomerInfo(selectedCustomer))
                {
                    f.ShowDialog();
                    PopulateData();
                }

            }
            else if (itemName.Equals(bbiDelete.Name))
            {
                CustomerDTO selectedCustomer = getSelectedCustomer();
                try
                {
                    DialogResult deleteResult = Utils.ShowAuthentication(MessageBundle.DELETE_WARNING_CONTENT.Value);
                    if (deleteResult == DialogResult.Yes)
                    {
                        customerDAO.Delete(selectedCustomer.Id);
                        LogUtil.log.Error("You have just deleted this Customer" + selectedCustomer.Id);
                    }

                }

                catch (System.Exception ex)
                {
                    LogUtil.log.Error("can not delete this unit" + selectedCustomer.Id);
                    LogUtil.log.Error(ex);
                }

                PopulateData();
            }

            else if (itemName.Equals(bbiExit.Name))
            {

            }
        }

      
    }
}
